package com.cwn.wizbank.entity.vo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class IdCardVO {
    private int age;
    private String gender;
    public static String ID_CARD_ERROR_MESSAGE = "身份证号长度错误";

    public IdCardVO(String idNumber) {
        if (idNumber == null || (idNumber.length() != 18 && idNumber.length() != 15)) {
            throw new IllegalArgumentException(ID_CARD_ERROR_MESSAGE);
        }
        //从身份证号码中提取年龄
        this.age = countAge(idNumber);
        // 从身份证号码中提取性别
        this.gender = judgeGender(idNumber);
    }

    public static String judgeGender(String idNumber) throws IllegalArgumentException {
        if (idNumber.length() != 18 && idNumber.length() != 15) {
            throw new IllegalArgumentException(ID_CARD_ERROR_MESSAGE);
        }
        int gender = 0;
        if (idNumber.length() == 18) {
            //如果身份证号18位，取身份证号倒数第二位
            char c = idNumber.charAt(idNumber.length() - 2);
            gender = Integer.parseInt(String.valueOf(c));
        } else {
            //如果身份证号15位，取身份证号最后一位
            char c = idNumber.charAt(idNumber.length() - 1);
            gender = Integer.parseInt(String.valueOf(c));
        }
        if (gender % 2 == 1) {
            return "男";
        } else {
            return "女";
        }
    }

    public static int countAge(String idNumber) {
        if (idNumber == null || (idNumber.length() != 18 && idNumber.length() != 15)) {
            throw new IllegalArgumentException(ID_CARD_ERROR_MESSAGE);
        }
        String year;
        String yue;
        String day;
        if (idNumber.length() == 18) {
            year = idNumber.substring(6).substring(0, 4);
            // 得到年份
            yue = idNumber.substring(10).substring(0, 2);
            // 得到月份
            day = idNumber.substring(12).substring(0, 2);
            //得到日
        } else {
            year = "19" + idNumber.substring(6, 8);
            // 年份
            yue = idNumber.substring(8, 10);
            // 月份
            day = idNumber.substring(10, 12);
            //日
        }
        Date date = new Date();
        // 得到当前的系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String fyear = format.format(date).substring(0, 4);
        // 当前年份
        String fyue = format.format(date).substring(5, 7);
        // 月份
        String fday = format.format(date).substring(8, 10);
        int age = 0;
        int age1 = Integer.parseInt(fyear) - Integer.parseInt(year);
        int age2 = Integer.parseInt(fyear) - Integer.parseInt(year) - 1;
        if (Integer.parseInt(yue) == Integer.parseInt(fyue)) {
            //如果月份相同 // 说明已经过了生日或者今天是生日
            if (Integer.parseInt(day) <= Integer.parseInt(fday)) {
                age = age1;
            } else {
                age = age2;
            }
        } else {
            if (Integer.parseInt(yue) < Integer.parseInt(fyue)) {
                //如果当前月份大于出生月份
                age = age1;
            } else {
                //如果当前月份小于出生月份,说明生日还没过
                age = age2;
            }
        }
        return Math.max(age, 0);
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public static void main(String[] args) {
        IdCardVO OverRage = new IdCardVO("123456789012345678"); // 示例身份证号码
        System.out.println("年龄：" + OverRage.getAge());
        System.out.println("性别：" + OverRage.getGender());
    }
}